package com.project.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.entity.BookInfo;
import com.project.entity.BookType;
import com.project.entity.ReaderInfo;

/**
 * @author BerylL@synnex.com
 * @version 1.0 by BerylL@synnex.com
 * @version 2.0 by travisl@synnex.com
 */
@Controller
@RequestMapping("/bookinfo")
public class BookInfoController extends GenericController {

	@RequestMapping(value = "/add")
	public String add(Model model) {
		List<BookType> list = bookTypeService.listAll();

		model.addAttribute("bookTypeList", list);

		return "bookinfo/edit";
	}

	@RequestMapping(value = "save")
	public String save(HttpSession session, BookInfo bookInfo, String btypeId) {
		BookType bookType = bookTypeService.get(Integer.parseInt(btypeId));
		BookInfo bookInfo2 = null;

		// add book type to bookInfo
		bookInfo.setBookType(bookType);

		if (bookInfo.getId() == null) {
			// verify this book exist or not, then save it by different method.
			bookInfo2 = (BookInfo) bookInfoService.verifyBookInfo(
					bookInfo.getName(), bookInfo.getAuthor(),
					bookInfo.getPublisher());
			if (bookInfo2 == null) {
				Date date = new Date();
				ReaderInfo readerInfo = (ReaderInfo) session
						.getAttribute("readerInfo");

				bookInfo.setStoreTime(date);
				bookInfo.setEntryReader(readerInfo);
				bookInfo.setEntryDateTime(date);
				bookInfoService.save(bookInfo);
			} else {
				int totalQuantity = bookInfo2.getTotalQuantity()
						+ bookInfo.getTotalQuantity();

				bookInfo2.setTotalQuantity(totalQuantity);
				bookInfoService.update(bookInfo2);
			}

		} else {
			bookInfo2 = bookInfoService.get(bookInfo.getId());
			// update this book info
			bookInfo.setStoreTime(bookInfo2.getStoreTime());
			bookInfo.setEntryReader(bookInfo2.getEntryReader());
			bookInfo.setEntryDateTime(bookInfo2.getEntryDateTime());
			bookInfoService.update(bookInfo);
		}

		return "redirect:/bookinfo/list";
	}

	@RequestMapping(value = "/update")
	public String update(String id, String btypeId, Model model) {
		BookInfo bookInfo = bookInfoService.get(Integer.parseInt(id));
		List<BookType> list = bookTypeService.listAll();

		model.addAttribute("bookInfo", bookInfo);
		model.addAttribute("btypeId", btypeId);
		model.addAttribute("bookTypeList", list);

		return "bookinfo/edit";
	}

	@RequestMapping(value = "/list")
	public String list(Model model) {
		List<BookType> bookTypeList = bookTypeService.listAll();

		model.addAttribute("bookTypeList", bookTypeList);

		return "bookinfo/list";
	}

	@RequestMapping(value = "/delete")
	public String delete(HttpSession session, String id) {
		BookInfo bookInfo = bookInfoService.get(Integer.parseInt(id));
		ReaderInfo readerInfo = (ReaderInfo) session.getAttribute("readerInfo");

		bookInfo.setDeleteReader(readerInfo);
		bookInfo.setDeleteDateTime(new Date());

		bookInfoService.update(bookInfo);

		return "redirect:/bookinfo/list";
	}

	@RequestMapping(value = "/search")
	@ResponseBody
	public Map<String, Object> search(@RequestBody Map<String, String> map) {
		Map<String, Object> hashMap = new HashMap<String, Object>();
		List<BookInfo> list = null;
		String bookName = map.get("bookName");
		Integer btypeId;
		Integer pageNow = Integer.parseInt(map.get("pageNow"));
		Integer pageSize = Integer.parseInt(map.get("pageSize"));
		Integer totalCount;

		if (StringUtils.isNumeric(map.get("btypeId"))) {
			btypeId = Integer.parseInt(map.get("btypeId"));
		} else {
			btypeId = 0;
		}

		list = bookInfoService.search(bookName, btypeId, pageNow, pageSize);
		totalCount = bookInfoService.totalCount(bookName, btypeId);
		hashMap.put("bookList", list);
		hashMap.put("totalCount", totalCount);

		return hashMap;
	}

}
